<!--
  智慧社区管理系统 - 访客管理页面
  @author Wu.Liang
  @date 2025-01-20
  @description 访客登记、审核、管理
-->
<template>
  <div class="security-visitor-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-title">
          <el-icon size="24" class="title-icon"><User /></el-icon>
          <h2>访客管理</h2>
        </div>
        <div class="header-actions">
          <el-button type="primary" @click="handleAddVisitor">
            <el-icon><Plus /></el-icon>
            新增访客
          </el-button>
          <el-button @click="handleRefresh">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </div>
    </div>

    <!-- 统计卡片 -->
    <div class="statistics-cards">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card total-visitors">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><User /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.totalVisitors }}</div>
                <div class="stat-label">访客总数</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card today-visitors">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Calendar /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.todayVisitors }}</div>
                <div class="stat-label">今日访客</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card pending-visitors">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Clock /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.pendingVisitors }}</div>
                <div class="stat-label">待审核</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card class="stat-card approved-visitors">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon size="32"><Check /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-number">{{ statistics.approvedVisitors }}</div>
                <div class="stat-label">已通过</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 搜索表单 -->
    <el-card class="search-card">
      <el-form :model="searchForm" :inline="true" class="search-form">
        <el-form-item label="访客姓名">
          <el-input
            v-model="searchForm.visitorName"
            placeholder="请输入访客姓名"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select
            v-model="searchForm.approvalStatus"
            placeholder="请选择审核状态"
            clearable
            style="width: 150px"
          >
            <el-option label="待审核" :value="0" />
            <el-option label="已通过" :value="1" />
            <el-option label="已拒绝" :value="2" />
            <el-option label="已过期" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="被访人">
          <el-input
            v-model="searchForm.hostName"
            placeholder="请输入被访人姓名"
            clearable
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="访问时间">
          <el-date-picker
            v-model="searchForm.visitTimeRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 350px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 访客列表 -->
    <el-card class="visitor-list-card">
      <template #header>
        <div class="card-header">
          <span>访客列表</span>
          <div class="header-actions">
            <el-button type="success" size="small" @click="handleBatchApprove">
              <el-icon><Check /></el-icon>
              批量通过
            </el-button>
            <el-button type="danger" size="small" @click="handleBatchReject">
              <el-icon><Close /></el-icon>
              批量拒绝
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="visitorList"
        v-loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="visitorName" label="访客姓名" width="120" />
        <el-table-column prop="phone" label="联系电话" width="130" />
        <el-table-column prop="idCard" label="身份证号" width="180" />
        <el-table-column prop="hostName" label="被访人" width="120" />
        <el-table-column prop="hostPhone" label="被访人电话" width="130" />
        <el-table-column prop="visitPurpose" label="访问目的" min-width="150" />
        <el-table-column prop="approvalStatus" label="审核状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getApprovalStatusTag(row.approvalStatus)">
              {{ getApprovalStatusName(row.approvalStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="visitTime" label="访问时间" width="180" />
        <el-table-column prop="leaveTime" label="离开时间" width="180" />
        <el-table-column prop="approvalUser" label="审核人" width="120" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              查看
            </el-button>
            <el-button type="success" size="small" @click="handleApprove(row)" v-if="row.approvalStatus === 0">
              <el-icon><Check /></el-icon>
              通过
            </el-button>
            <el-button type="danger" size="small" @click="handleReject(row)" v-if="row.approvalStatus === 0">
              <el-icon><Close /></el-icon>
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 访客详情弹窗 -->
    <el-dialog
      v-model="visitorDetailVisible"
      title="访客详情"
      width="800px"
      :close-on-click-modal="false"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="访客姓名">{{ currentVisitor.visitorName }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ currentVisitor.phone }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ currentVisitor.idCard }}</el-descriptions-item>
        <el-descriptions-item label="车牌号">{{ currentVisitor.carNumber || '无' }}</el-descriptions-item>
        <el-descriptions-item label="被访人">{{ currentVisitor.hostName }}</el-descriptions-item>
        <el-descriptions-item label="被访人电话">{{ currentVisitor.hostPhone }}</el-descriptions-item>
        <el-descriptions-item label="被访地址">{{ currentVisitor.hostAddress }}</el-descriptions-item>
        <el-descriptions-item label="访问目的">{{ currentVisitor.visitPurpose }}</el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <el-tag :type="getApprovalStatusTag(currentVisitor.approvalStatus)">
            {{ getApprovalStatusName(currentVisitor.approvalStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="访问时间">{{ currentVisitor.visitTime }}</el-descriptions-item>
        <el-descriptions-item label="离开时间">{{ currentVisitor.leaveTime || '未离开' }}</el-descriptions-item>
        <el-descriptions-item label="审核人">{{ currentVisitor.approvalUser || '未审核' }}</el-descriptions-item>
        <el-descriptions-item label="审核时间">{{ currentVisitor.approvalTime || '未审核' }}</el-descriptions-item>
        <el-descriptions-item label="审核备注" :span="2">{{ currentVisitor.approvalRemark || '暂无备注' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentVisitor.remark || '暂无备注' }}</el-descriptions-item>
      </el-descriptions>

      <template #footer>
        <el-button @click="visitorDetailVisible = false">关闭</el-button>
        <el-button type="success" @click="handleApprove(currentVisitor)" v-if="currentVisitor.approvalStatus === 0">通过</el-button>
        <el-button type="danger" @click="handleReject(currentVisitor)" v-if="currentVisitor.approvalStatus === 0">拒绝</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { showMessage } from '@/utils/message'
import {
  User,
  Plus,
  Refresh,
  Calendar,
  Clock,
  Check,
  Search,
  Close,
  View
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const visitorDetailVisible = ref(false)
const selectedVisitors = ref<any[]>([])

// 搜索表单
const searchForm = reactive({
  visitorName: '',
  approvalStatus: undefined as number | undefined,
  hostName: '',
  visitTimeRange: [] as string[]
})

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 20,
  total: 0
})

// 统计数据
const statistics = reactive({
  totalVisitors: 1256,
  todayVisitors: 23,
  pendingVisitors: 8,
  approvedVisitors: 1248
})

// 当前访客详情
const currentVisitor = ref<any>({})

// 访客列表数据
const visitorList = ref([
  {
    id: 1,
    visitorName: '张三',
    phone: '13800138001',
    idCard: '110101199001011234',
    carNumber: '京A12345',
    hostName: '李四',
    hostPhone: '13900139001',
    hostAddress: '1号楼101室',
    visitPurpose: '探亲访友',
    approvalStatus: 0,
    visitTime: '2025-01-20 15:30:00',
    leaveTime: null,
    approvalUser: null,
    approvalTime: null,
    approvalRemark: null,
    remark: '首次来访'
  },
  {
    id: 2,
    visitorName: '王五',
    phone: '13800138002',
    idCard: '110101199002022345',
    carNumber: null,
    hostName: '赵六',
    hostPhone: '13900139002',
    hostAddress: '2号楼203室',
    visitPurpose: '送快递',
    approvalStatus: 1,
    visitTime: '2025-01-20 14:00:00',
    leaveTime: '2025-01-20 14:15:00',
    approvalUser: '保安小王',
    approvalTime: '2025-01-20 13:55:00',
    approvalRemark: '快递员，正常放行',
    remark: '快递配送'
  },
  {
    id: 3,
    visitorName: '孙七',
    phone: '13800138003',
    idCard: '110101199003033456',
    carNumber: '京B67890',
    hostName: '周八',
    hostPhone: '13900139003',
    hostAddress: '3号楼305室',
    visitPurpose: '装修施工',
    approvalStatus: 2,
    visitTime: '2025-01-20 16:00:00',
    leaveTime: null,
    approvalUser: '物业经理',
    approvalTime: '2025-01-20 15:45:00',
    approvalRemark: '装修时间不符合规定，拒绝进入',
    remark: '装修工人'
  },
  {
    id: 4,
    visitorName: '吴九',
    phone: '13800138004',
    idCard: '110101199004044567',
    carNumber: null,
    hostName: '郑十',
    hostPhone: '13900139004',
    hostAddress: '1号楼102室',
    visitPurpose: '朋友聚会',
    approvalStatus: 1,
    visitTime: '2025-01-20 18:00:00',
    leaveTime: '2025-01-20 22:00:00',
    approvalUser: '保安小李',
    approvalTime: '2025-01-20 17:50:00',
    approvalRemark: '业主确认，正常放行',
    remark: '朋友聚会'
  },
  {
    id: 5,
    visitorName: '陈十一',
    phone: '13800138005',
    idCard: '110101199005055678',
    carNumber: '京C11111',
    hostName: '刘十二',
    hostPhone: '13900139005',
    hostAddress: '2号楼204室',
    visitPurpose: '商务洽谈',
    approvalStatus: 0,
    visitTime: '2025-01-20 19:00:00',
    leaveTime: null,
    approvalUser: null,
    approvalTime: null,
    approvalRemark: null,
    remark: '商务合作'
  }
])

// 获取审核状态标签样式
const getApprovalStatusTag = (status: number): 'warning' | 'success' | 'danger' | 'info' => {
  const statusMap: Record<number, 'warning' | 'success' | 'danger' | 'info'> = {
    0: 'warning',  // 待审核
    1: 'success',  // 已通过
    2: 'danger',   // 已拒绝
    3: 'info'      // 已过期
  }
  return statusMap[status] || 'warning'
}

// 获取审核状态名称
const getApprovalStatusName = (status: number): string => {
  const statusMap: Record<number, string> = {
    0: '待审核',
    1: '已通过',
    2: '已拒绝',
    3: '已过期'
  }
  return statusMap[status] || '未知状态'
}

// 搜索处理
const handleSearch = () => {
  pagination.currentPage = 1
  loadVisitorList()
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    visitorName: '',
    approvalStatus: undefined,
    hostName: '',
    visitTimeRange: []
  })
  handleSearch()
}

// 加载访客列表
const loadVisitorList = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    loading.value = false
    pagination.total = visitorList.value.length
  }, 500)
}

// 新增访客
const handleAddVisitor = () => {
  showMessage({ type: 'info', message: '新增访客功能开发中' })
}

// 刷新
const handleRefresh = () => {
  loadVisitorList()
  showMessage({ type: 'success', message: '数据已刷新' })
}

// 查看访客详情
const handleView = (row: any) => {
  currentVisitor.value = { ...row }
  visitorDetailVisible.value = true
}

// 通过访客
const handleApprove = (row: any) => {
  showMessage({ type: 'info', message: '通过访客功能开发中' })
}

// 拒绝访客
const handleReject = (row: any) => {
  showMessage({ type: 'info', message: '拒绝访客功能开发中' })
}

// 批量通过
const handleBatchApprove = () => {
  if (selectedVisitors.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要操作的访客' })
    return
  }
  showMessage({ type: 'info', message: '批量通过功能开发中' })
}

// 批量拒绝
const handleBatchReject = () => {
  if (selectedVisitors.value.length === 0) {
    showMessage({ type: 'warning', message: '请选择要操作的访客' })
    return
  }
  showMessage({ type: 'info', message: '批量拒绝功能开发中' })
}

// 选择变化处理
const handleSelectionChange = (selection: any[]) => {
  selectedVisitors.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  pagination.currentPage = 1
  loadVisitorList()
}

// 当前页变化
const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  loadVisitorList()
}

// 页面初始化
onMounted(() => {
  loadVisitorList()
})
</script>

<style scoped lang="scss">
.security-visitor-container {
  padding: 20px;
  background: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);

    .header-content {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .header-title {
        display: flex;
        align-items: center;
        color: white;

        .title-icon {
          margin-right: 12px;
        }

        h2 {
          margin: 0;
          font-size: 24px;
          font-weight: 600;
        }
      }

      .header-actions {
        display: flex;
        gap: 12px;
      }
    }
  }

  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: transform 0.3s ease;

      &:hover {
        transform: translateY(-2px);
      }

      .stat-content {
        display: flex;
        align-items: center;
        padding: 8px;

        .stat-icon {
          margin-right: 16px;
          padding: 12px;
          border-radius: 8px;
          color: white;
        }

        .stat-info {
          .stat-number {
            font-size: 28px;
            font-weight: bold;
            line-height: 1;
            margin-bottom: 4px;
          }

          .stat-label {
            font-size: 14px;
            color: #666;
          }
        }
      }

      &.total-visitors .stat-icon {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      &.today-visitors .stat-icon {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      &.pending-visitors .stat-icon {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      &.approved-visitors .stat-icon {
        background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      }
    }
  }

  .search-card {
    margin-bottom: 20px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .search-form {
      .el-form-item {
        margin-bottom: 16px;
      }
    }
  }

  .visitor-list-card {
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-weight: 600;
      font-size: 16px;

      .header-actions {
        display: flex;
        gap: 8px;
      }
    }

    .pagination-container {
      display: flex;
      justify-content: center;
      margin-top: 20px;
      padding-top: 20px;
      border-top: 1px solid #ebeef5;
    }
  }
}
</style> 